<?php

namespace App\Http\Requests\Api;

use App\Models\Schedule;
use Dingo\Api\Http\FormRequest;

class ScheduleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        if ($this->isMethod('GET')) {
            return [
                'goods_id' => [
                    'exists:products,id',
                    'required_with:goods_type',
                ],
                'goods_type' => [
                    'in:product',
                    'required_with:goods_id',
                ],
            ];
        }

        if ($this->isMethod('POST')) {
            return [
                'goods_id' => [
                    'required',
                    'exists:products,id',
                ],
                'goods_type' => [
                    'required',
                    'in:product',
                ],
                'price' => [
                    'required',
                    'numeric',
                ],
                'begin_time' => [
                    'required',
                    'date_format:Y-m-d H:i',
                    function ($attribute, $value, $fail) {
                        if ($value <= date('Y-m-d H:i:s')) {
                            $fail('begin_time or end_time could not earlier than now');

                            return;
                        }
                    },
                ],
                'end_time' => [
                    'required',
                    'date_format:Y-m-d H:i',
                    function ($attribute, $value, $fail) {
                        if ($value <= date('Y-m-d H:i:s')) {
                            $fail('begin_time or end_time could not earlier than now');

                            return;
                        }

                        if ($this->begin_time >= $value) {
                            $fail('begin_time must be earlier than end_time');

                            return;
                        }
                    },
                ],

            ];
        }

        if ($this->isMethod('PUT')) {
            return [
                'id' => [
                    'required',
                    'exists:schedules,id',
                ],
                'price' => [
                    'numeric',
                ],
                'begin_time' => [
                    'required',
                    'date_format:Y-m-d H:i',
                ],
                'end_time' => [
                    'required',
                    'date_format:Y-m-d H:i',
                    function ($attribute, $value, $fail) {
                        if ($this->begin_time >= $value) {
                            $fail('begin_time can not be smaller than end_time');

                            return;
                        }
                    },
                ],
            ];
        }

        if ($this->isMethod('DELETE')) {
            return [
                'id' => [
                    'required',
                    'exists:schedules,id',
                    function ($attribute, $value, $fail) {
                        $schedule = Schedule::find($value);

                        if ($schedule->status == 1) {
                            $fail($attribute . ' ' . $value . ' which schedule is occupied');

                            return;
                        }
                    },
                ],
            ];
        }
    }

}
